<?php

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require(ROOT_PATH . 'includes/lib_payment.php');
require(ROOT_PATH . 'includes/lib_order.php');

$cappay = new cappay();
$act = $_GET['act'];
if ($act == 'post_data'){
	$cappay->post_data();
}else{
	echo $cappay->get_code($order, $payment);
}

class cappay
{
    /**
     * 构造函数
     *
     * @access  public
     * @param
     *
     * @return void
     */
    function cappay()
    {
    }

    function __construct()
    {
        $this->cappay();
    }

    /**
     * 生成支付代码
     * @param   array   $order      订单信息
     * @param   array   $payment    支付方式信息
     */
    function get_code($order, $payment)
    {
    	$v_mid = '10514';
    	$v_oid = '';
    	$v_refamount = 0.01;
    	$v_refoprt = '47180';
    	$v_refreason = 'test';
        
        $def_url = '<table border=1 align=center width="500"><tr><td colspan="2"><h3 align="center">申请退款</h3></td></tr>';
        $def_url .= '<form action="paytest_refund.php?act=post_data" method="post">';
        $def_url .= '<tr><td>商户号</td><td><input type="text" name="v_mid" value="'.$v_mid.'"></td></tr>';
        $def_url .= '<tr><td>订单编号</td><td><input type="text" name="v_oid" value="'.$v_oid.'"></td></tr>';
        $def_url .= '<tr><td>退款金额</td><td><input type="text" name="v_refamount" value="'.$v_refamount.'"></td></tr>';
        $def_url .= '<tr><td>退款原因</td><td><input type="text" name="v_refreason"  value="'.$v_refreason.'"></td></tr>';
        $def_url .= '<tr><td>操作员编号</td><td><input type="text" name="v_refoprt" value="'.$v_refoprt.'" ></td></tr>';
        $def_url .= '<tr><td align="center" colspan="2"><input type="submit" value="提交"></td></tr>';
        $def_url .= '</form></table>';

        return $def_url;
    }

    function post_data()
    {
    	$v_mid = $_POST['v_mid'];
    	$v_oid = $_POST['v_oid'];
    	$v_refamount = $_POST['v_refamount'];
    	$v_refreason = $_POST['v_refreason'];
    	$v_refoprt = $_POST['v_refoprt'];
    	$data = $v_mid.$v_oid.$v_refamount.$v_refoprt;
    	$v_mac = $this->hmac_md5('test', $data);
    	$curlPost = '';
    	$url = "https://api.yizhifubj.com/merchant/refund/ref_ack_submit.jsp";
    	$curlPost.="v_mid=".$v_mid;
    	$curlPost.="&v_oid=".$v_oid;
    	$curlPost.="&v_refamount=".$v_refamount;
    	$curlPost.="&v_refreason=".$v_refreason;
    	$curlPost.="&v_refoprt=".$v_refoprt;
    	$curlPost.="&v_mac=".$v_mac;
    	    	echo $curlPost;
    	
//     	$header = array("Accept-Charset: utf-8","Content-type: application/x-www-form-urlencoded");
    	$ch=curl_init($url.'?'.$curlPost);
//     	curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //设置头信息的地方
//     	curl_setopt($ch, CURLOPT_URL, $url);
//     	curl_setopt($ch, CURLOPT_POST, 1);
//     	curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    	$back_date=trim(curl_exec($ch));
    	curl_close($ch);
    	$xml = simplexml_load_string($back_date);
    	var_dump($xml);
    }
    function hmac_md5($key, $data)
    {
        if (extension_loaded('mhash'))
        {
            return bin2hex(mhash(MHASH_MD5, $data, $key));
        }

        // RFC 2104 HMAC implementation for php. Hacked by Lance Rushing
        $b = 64;
        if (strlen($key) > $b)
        {
            $key = pack('H*', md5($key));
        }
        $key  = str_pad($key, $b, chr(0x00));
        $ipad = str_pad('', $b, chr(0x36));
        $opad = str_pad('', $b, chr(0x5c));

        $k_ipad = $key ^ $ipad;
        $k_opad = $key ^ $opad;

        return md5($k_opad . pack('H*', md5($k_ipad . $data)));
    }

}

?>